﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataAdapter与DataSet的使用
{
    public partial class GridView : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblInfo.Text = (GridView1.SelectedIndex).ToString();
            //GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.LightPink;

            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    if (row == GridView1.SelectedRow)
            //    {
            //        row.BackColor = Color.LightPink;
            //    }
            //    else
            //    {
            //        row.BackColor = Color.Transparent;
            //    }
            //}
        }

        private void BindGridView()
        {
            //-----------------------使用DataAdapter+DataSet显示数据-----------------
            //using (
            //    SqlConnection connection =
            //        new SqlConnection("data source=10.22.149.99;database=School;uid=Student;pwd=123456"))
            //{
            //    SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM Student", connection);
            //    DataSet dataSet = new DataSet();
            //    dataAdapter.Fill(dataSet, "Student");
            //    GridView1.DataSource = dataSet;
            //    GridView1.DataBind();
            //}
            //-----------------------使用DataAdapter+DataSet显示数据-----------------


            //-----------------------使用DataSource显示数据-----------------
            var dataSource = new SqlDataSource();
            dataSource.ConnectionString = "data source=10.22.149.99;database=School;uid=Student;pwd=123456";
            dataSource.SelectCommand = "SELECT * FROM Student";
            dataSource.DeleteCommand = "DELETE FROM Student WHERE StudentID=@StudentID";
            GridView1.DataSource = dataSource;
            GridView1.DataBind();


            //-----------------------使用DataSource显示数据-----------------
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGridView();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGridView();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //-------------------------------使用SQL语句删除----------------------
            string id = GridView1.Rows[e.RowIndex].Cells[3].Text;
            //GridView1.DataKeyNames = new string[] {"StudentId"};
            lblInfo.Text = id;

            using (
                var connection =
                    new SqlConnection("data source=10.22.149.99;database=School;uid=Student;pwd=123456"))
            {
                string commanText = string.Format("DELETE FROM Student WHERE StudentID = '{0}'", id);
                var command = new SqlCommand(commanText, connection);
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                command.ExecuteNonQuery();
                BindGridView();
            }
            //-------------------------------使用SQL语句删除----------------------
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // lblInfo.Text = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        }
    }
}